package guda.sqladmin.biz.impl;

import java.util.List;

import guda.sqladmin.biz.query.FieldQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import guda.sqladmin.biz.TableFieldBiz;
import guda.sqladmin.dao.TableFieldDOMapper;
import guda.sqladmin.dao.domain.TableFieldDO;
import guda.sqladmin.dao.domain.TableFieldDOCriteria;
import guda.tools.web.page.BaseQuery;
import guda.tools.web.page.BizResult;

public class TableFieldBizImpl implements TableFieldBiz{

    private final static Logger logger = LoggerFactory.getLogger(TableFieldBizImpl.class);

    @Autowired
    private TableFieldDOMapper tableFieldDOMapper;

    public BizResult detail(long id) {
        BizResult bizResult = new BizResult();
        try{
            TableFieldDO tableFieldDO = tableFieldDOMapper.selectByPrimaryKey(id);
            bizResult.data.put("tableFieldDO", tableFieldDO);
            bizResult.success = true;
        }catch(Exception e){
            logger.error("query TableField error",e);
        }
        return bizResult;
}

    public BizResult list(FieldQuery fieldQuery) {
        BizResult bizResult = new BizResult();
        try {
            TableFieldDOCriteria tableFieldDOCriteria = new TableFieldDOCriteria();
            tableFieldDOCriteria.setStartRow(fieldQuery.getStartRow());
            tableFieldDOCriteria.setPageSize(fieldQuery.getPageSize());
            tableFieldDOCriteria.createCriteria().andTableIdEqualTo(fieldQuery.getTableId());
            int totalCount = tableFieldDOMapper.countByExample(tableFieldDOCriteria);
            fieldQuery.setTotalCount(totalCount);
            List<TableFieldDO> tableFieldDOList = tableFieldDOMapper.selectByExample(tableFieldDOCriteria);
            bizResult.data.put("tableFieldDOList", tableFieldDOList);
            bizResult.data.put("query", fieldQuery);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("view TableField list error", e);
        }
            return bizResult;
     }

    public BizResult delete(long id) {
        BizResult bizResult = new BizResult();
        try {
            tableFieldDOMapper.deleteByPrimaryKey(id);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("delete tableField error", e);
        }
        return bizResult;
    }

    public BizResult create(TableFieldDO tableFieldDO) {
        BizResult bizResult = new BizResult();
        try {
            int count = tableFieldDOMapper.insert(tableFieldDO);
            bizResult.data.put("count", count);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("create TableField error", e);
        }
        return bizResult;
    }

    public BizResult update(TableFieldDO tableFieldDO) {
        BizResult bizResult = new BizResult();
        try {
            int count = tableFieldDOMapper.updateByPrimaryKeySelective(tableFieldDO);
            bizResult.data.put("count", count);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("update TableField error", e);
        }
        return bizResult;
    }

    }
